﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DrugStore.Helper
{
    public class PaginateList<T>:List<T>
    {
        public int PageIndex { get; private set;}
        public int PageSize { get; private set;}
        public int TotalCount { get; private set;}
        public int TotalPages { get; private set; }
        public String KeyWord { get; private set; }
        public String OrderBy { get; private set; }
        public String Direction { get; private set; }
        public String Field { get; private set; }
        public String ID { get; set; }

        public PaginateList(IQueryable<T> source,int pageIndex,int pageSize,String keyWord,String field,String orderBy,String direction)
        {            
            PageSize = pageSize;
            TotalCount = source.Count();
            Direction = direction;
            OrderBy = orderBy;
            KeyWord = keyWord;
            Field = field;
            TotalPages = (int)Math.Ceiling(TotalCount / (double)pageSize);
            TotalPages = TotalPages > 0 ? TotalPages : 1;
            PageIndex = pageIndex <= 0 ? 1 : pageIndex;
            if (pageIndex > TotalPages)
                pageIndex = TotalPages;
            else PageIndex = pageIndex;
            this.AddRange(source.Skip((PageIndex - 1) * PageSize).Take(PageSize));
        }

        public bool HasPreviousPage 
        {
            get {
                return PageIndex < TotalPages;
            }
        }

        public bool HasNextPage 
        {
            get 
            {
                return PageIndex > 1;
            }
        }

        public PaginateList() { }
    }
}